package com.longteng.lesson2.controller;

import com.longteng.lesson2.domain.User;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Controller
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("/request")
    public @ResponseBody
    String header(HttpServletRequest httpServletRequest) {
       String ip = httpServletRequest.getRemoteHost();
       httpServletRequest.getHeader("");
       return ip;
    }
    @RequestMapping("/param1")
    public @ResponseBody String p(String name,Integer age){
      return name;
    }
    @RequestMapping("/paramUser")
    public @ResponseBody
    User u(User user){
        return user;
    }
    @RequestMapping("/requestParam")
    public @ResponseBody String requestParam(
            @RequestParam(value = "n",required = false,defaultValue = "我是requestParam") String name,
            Integer age){
        return name;
    }
    @RequestMapping(value = "/bodyParam",method = RequestMethod.POST)
    public @ResponseBody Object bodyParam(HttpServletRequest request){
        String body=null;
        try {
           BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(request.getInputStream()));
           body = IOUtils.toString(bufferedReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;
    }
    @RequestMapping(value = "/bodyParamUser",method = RequestMethod.POST)
    public @ResponseBody Object bodyParamUser(@RequestBody User user){
        return user;
    }
    @RequestMapping(value = "/bodyParamStr")
    public @ResponseBody Object bodyParamStr(@RequestBody String str){
        return str;
    }

}
